home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / telecom / 107 / c / cspifadj.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-02-20  |  13.2 KB  |  261 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /* Module:     cspifadj.c - line adjustment functions for cspiff            */
  4. /*                                                                          */
  5. /* Programmer: George R. Woodside                                           */
  6. /*                                                                          */
  7. /* Date:       October 25, 1986                                             */
  8. /*                                                                          */
  9. /* Function:   Perform alignment functions on program lines.                */
  10. /*                                                                          */
  11. /****************************************************************************/
  12.  
  13. #include <stdio.h>
  14.  
  15. extern  int  left;                      /* left column for comments         */
  16. extern  int  right;                     /* right column for comments        */
  17. extern  int  tab;                       /* step value for indentations      */
  18. extern  int  align;                     /* number of steps to indent        */
  19. extern  int  c_flag;                    /* continued comment line flag      */
  20.  
  21. /****************************************************************************/
  22. /*                                                                          */
  23. /* Construct a solid line of asterisks, as a comment line.                  */
  24. /*                                                                          */
  25. /****************************************************************************/
  26.  
  27. comm_solid(out)
  28. char out[];
  29. {
  30.   register int  i = 0;                  /* pointer index                    */
  31.   out[i++] = '/';                       /* leading slash                    */
  32.   while(i < right-1)
  33.     out[i++] = '*';                     /* pad the line                     */
  34.   out[i++] = '/';                       /* trailing slash                   */
  35.   out[i] = '\0';                        /* terminator                       */
  36. }                                       /* end solid line                   */
  37.  
  38. /****************************************************************************/
  39. /*                                                                          */
  40. /* Construct a solid block of asterisks, from the current                   */
  41. /* indentation column to the right margin, as a comment line.               */
  42. /*                                                                          */
  43. /****************************************************************************/
  44.  
  45. comm_block(out,in)
  46. char out[];
  47. char in[];
  48. {
  49.   register int  i= -1;                  /* pointer index                    */
  50.  
  51.   while(in[++i] != '*')
  52.     out[i] = in[i];                     /* copy up to block                 */
  53.  
  54.   while(i < right-1)
  55.     out[i++] = '*';                     /* pad the line                     */
  56.   out[i++] = '/';                       /* trailing slash                   */
  57.   out[i] = '\0';                        /* terminator                       */
  58. }                                       /* end solid line                   */
  59.  
  60. /****************************************************************************/
  61. /*                                                                          */
  62. /* Adjust the comment line in 'in' to force the right edge to 'right'       */
  63. /* if possible. If it is too long, allow it to extend past 'right'.         */
  64. /*                                                                          */
  65. /****************************************************************************/
  66.  
  67. comm_fix(out,in)
  68. char out[];
  69. char in[];
  70. {
  71.   register int i = -1;
  72.   register int j;
  73.  
  74.   j = ecomm(in);                        /* find end of comment area         */
  75.  
  76.   if( j == -1 )                         /* if no end of comment,            */
  77.     {
  78.       c_flag = 1;                       /* set continued comment flag       */
  79.       while(in[++i])                    /* for the length of the line,      */
  80.         out[i] = in[i];                 /* copy the comment                 */
  81.       out[i] = '\0';                    /* add the terminator               */
  82.       return(0);                        /* and get out                      */
  83.     }                                   /* end process unterminated comment */
  84.  
  85.   for( i=0; i<=j; i++ )
  86.     out[i] = in[i];                     /* copy significant portion of line */
  87.  
  88.   while(i < right-2)
  89.     out[i++] = ' ';                     /* pad the line                     */
  90.   out[i++] = '*';                       /* trailing asterisk                */
  91.   out[i++] = '/';                       /* trailing slash                   */
  92.   out[i] = '\0';                        /* terminator                       */
  93.  
  94. }                                       /* end comment fix                  */
  95.  
  96. /****************************************************************************/
  97. /*                                                                          */
  98. /* Adjust the comment line in 'in' to align the left edge with the          */
  99. /* prior (unterminated) comment line.                                       */
  100. /*                                                                          */
  101. /****************************************************************************/
  102.  
  103. comm_sync(out,in)
  104. char out[];
  105. char in[];
  106. {
  107.   register int i = -1;
  108.   register int j;
  109.  
  110.   while(in[++i])                        /* for the length of the line,      */
  111.     out[i] = in[i];                     /* copy the comment                 */
  112.   out[i] = '\0';                        /* add the terminator               */
  113.  
  114.   j = ecomm(in);                        /* find end of comment area         */
  115.  
  116.   if( j != -1 )                         /* if comments end,                 */
  117.     c_flag = 0;                         /* set off continued flag           */
  118. }                                       /* end comment fix                  */
  119.  
  120. /****************************************************************************/
  121. /*                                                                          */
  122. /* Adjust the start position of the code in 'in' to be aligned              */
  123. /* 'align' steps of 'tab'. Return aligned code in 'out'.                    */
  124. /*                                                                          */
  125. /****************************************************************************/
  126.  
  127. code_left(out,in)
  128. char  in[];                             /* input line                       */
  129. char  out[];                            /* output line                      */
  130. {
  131.   register  int  i;
  132.   register  int  j = 0;
  133.  
  134.   for( i = 0; i < align*tab; i++)
  135.     out[i] = ' ';                       /* pad to alignment position        */
  136.   out[i] = '\0';                        /* add a terminator                 */
  137.   while(in[j] == ' ')                   /* skip leading spaces              */
  138.     j++;                                /* but stay at first data byte      */
  139.   strcat(out,&in[j]);                   /* add text to pad                  */
  140. }                                       /* end code_left                    */
  141.  
  142. /****************************************************************************/
  143. /*                                                                          */
  144. /* Align the comment portion of 'in' to fit from 'left' to right',          */
  145. /* or as close as possible, then return it in 'out'.                        */
  146. /*                                                                          */
  147. /****************************************************************************/
  148.  
  149. comm_align(out,in)
  150. char  in[];                             /* input line                       */
  151. char  out[];                            /* output line                      */
  152. {
  153.   register  int  i = 0;                 /* general purpose                  */
  154.   register  int  j = 0;                 /* general purpose                  */
  155.   register  int  flag = 0;              /* loop trigger                     */
  156.   register  int  dend = -1;             /* end of data segment              */
  157.   register  int  cstart;                /* start of comment                 */
  158.   register  int  clen;                  /* length of comment segment        */
  159.   register  int  c;                     /* character image                  */
  160.  
  161.   /**************************************************************************/
  162.   /*                                                                        */
  163.   /* First, copy code segment of line to output array.                      */
  164.   /* Save the address of the second trailing space.                         */
  165.   /*                                                                        */
  166.   /**************************************************************************/
  167.  
  168.   while( (flag == 0) && (c = in[i]) )
  169.     {
  170.       if(c == '"')                      /* if we hit a quote,               */
  171.         {
  172.           out[i++] = c;                 /* copy the opening quote           */
  173.           while(( c = in[i] ) && (c != '"')) /* until end of quote or line  */
  174.             out[i++] = c;               /* copy the quoted byte             */
  175.  
  176.         }                               /* end quoted string                */
  177.  
  178.       if( (c == '/') && (in[i+1] == '*') ) /* if we are at the start,       */
  179.         {
  180.           flag = 1;                     /* got the comment                  */
  181.           cstart = i;                   /* save start position              */
  182.         }
  183.       else                              /* not at comment yet               */
  184.         {
  185.           if(c != ' ')
  186.             dend = i;                   /* save data byte address           */
  187.           out[i++] = c;                 /* copy the byte                    */
  188.         }                               /* end not at comment yet           */
  189.       }                                 /* end of line                      */
  190.  
  191.     if(flag == 0)                       /* did not locate comment           */
  192.       {
  193.         out[i] = '\0';                  /* insure terminated                */
  194.         return(0);                      /* end of line, no comment          */
  195.       }
  196.  
  197.     i = dend + 1;                       /* back up to data                  */
  198.     flag = ecomm(in);                   /* find end of comment              */
  199.  
  200.     if(flag == -1)                      /* if bad syntax,                   */
  201.       return(0);                        /* give up on line.                 */
  202.  
  203.     clen = flag - cstart +1;            /* compute comment length           */
  204.  
  205.     if(dend < left)                     /* if data ends soon enough,        */
  206.       j = left;                         /* start at normal position.        */
  207.     else                                /* if data is too long,             */
  208.       j = dend+2;                       /* leave one space after it.        */
  209.  
  210.     while(i < j)
  211.       out[i++] = ' ';                   /* blank fill to comment location.  */
  212.  
  213.     while(clen)
  214.       {
  215.         out[i++] = in[cstart++];        /* now copy comment                 */
  216.         clen--;                         /* for whatever its length is       */
  217.       }
  218.  
  219.     do {
  220.       out[i++] = ' ';                   /* insure at least one space        */
  221.       } while(i < right-2);             /* pad to right edge                */
  222.  
  223.     out[i++] = '*';                     /* put in asterisk                  */
  224.     out[i++] = '/';                     /* and slash                        */
  225.     out[i]   = '\0';                    /* and terminate                    */
  226. }
  227.  
  228. /****************************************************************************/
  229. /*                                                                          */
  230. /* Determine address of last data character in comment portion              */
  231. /* of line. Skip trailing asterisk, slash, and white space.                 */
  232. /* Return index of last data character, or -1 if not found.                 */
  233. /*                                                                          */
  234. /****************************************************************************/
  235.  
  236. ecomm(line)
  237. char  line[];
  238. {
  239.   register  int   i;
  240.   register  int   flag = 0;
  241.  
  242.   i = strlen(line) -1 ;                 /* find end of line                 */
  243.  
  244.   while( (flag == 0) && (i > 3) )
  245.     {
  246.       if( (line[i] == '/') && (line[i-1] == '*') )
  247.         {
  248.           flag = 1;                     /* got end of comment symbol        */
  249.           i-= 2;                        /* get behind asterisk              */
  250.           while( (i) && (line[i] == ' ') )
  251.             i--;                        /* back up to data                  */
  252.         }
  253.       else
  254.         i--;                            /* keep looking                     */
  255.     }
  256.   if(flag)                              /* if we found the end,             */
  257.     return(i);                          /* return it.                       */
  258.   else                                  /* if not,                          */
  259.     return(-1);                         /* return not found flag.           */
  260. }
  261.